/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.stock.entity.*;
import com.thinkgem.jeesite.modules.stock.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;

import java.util.List;

/**
 * 出库Controller
 * @author 出库
 * @version 2019-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/stock/outOrder")
public class OutOrderController extends BaseController {

	@Autowired
	private OutOrderService outOrderService;
	@Autowired
	private WoodsService woodsService;
	@ModelAttribute
	public OutOrder get(@RequestParam(required=false) String id) {
		OutOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = outOrderService.get(id);
		}
		if (entity == null){
			entity = new OutOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("stock:outOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(OutOrder outOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OutOrder> page = outOrderService.findPage(new Page<OutOrder>(request, response), outOrder); 
		model.addAttribute("page", page);
		return "modules/stock/outOrderList";
	}

	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private StockService stockService;

	@RequiresPermissions("stock:outOrder:edit")
	@RequestMapping(value = "saveAudit")
	public String saveAudit(OutOrder outOrder, Model model, RedirectAttributes redirectAttributes) {
		if (org.apache.commons.lang3.StringUtils.isBlank(outOrder.getAct().getFlag())
				|| org.apache.commons.lang3.StringUtils.isBlank(outOrder.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return form(outOrder, model);
		}
		outOrderService.auditSave(outOrder);
		return "redirect:" + adminPath + "/act/task/todo/";
	}

	@Autowired
	private ContainerDetailService containerDetailService;
	@RequiresPermissions("stock:outOrder:view")
	@RequestMapping(value = "view")
	public String view(OutOrder outOrder, Model model) {
		model.addAttribute("outOrder", outOrder);
		return "modules/stock/outOrderView";
	}
	@RequiresPermissions("stock:outOrder:view")
	@RequestMapping(value = "form")
	public String form(OutOrder outOrder, Model model) {
		List<Woods> woods= woodsService.findList(new Woods());
		model.addAttribute("woods",woods);


		if (org.apache.commons.lang3.StringUtils.isNotBlank(outOrder.getId())){
			// 环节编号
			String taskDefKey = outOrder.getAct().getTaskDefKey();
			model.addAttribute("outOrder", outOrder);
			if(taskDefKey.equals("audit1")){
				List<OutOrderDetail> ods=outOrder.getOutOrderDetailList();
				for (OutOrderDetail o:ods
					 ) {
					Stock stock=new Stock();
					stock.setW(o.getWoods());
					List<Stock>ss =stockService.findList(stock);
					if(ss!=null&&ss.size()>0){
						stock=ss.get(0);
						o.setFactNumber(stock.getNumber());
					}
				}



				return "modules/stock/outOrderaudit1Form";
			}
			else if(taskDefKey.equals("audit2")){
				/*List warehouses= warehouseService.findList(new Warehouse());
				model.addAttribute("warehouses",warehouses);*/
				List<OutOrderDetail> ods=outOrder.getOutOrderDetailList();
				for (OutOrderDetail o:ods
						) {
					ContainerDetail containerDetail=new ContainerDetail();
					containerDetail.setWoodsid(o.getWoods());
					o.setWls(containerDetailService.findList(containerDetail));
				}
				return "modules/stock/outOrderaudit2Form";
			}
			else if(taskDefKey.equals("audit3")){

				return "modules/stock/outOrderaudit3Form";
			}
			else if(taskDefKey.equals("audit4")){

				return "modules/stock/outOrderaudit4Form";
			}else if(taskDefKey.equals("audit5")){

				return "modules/stock/outOrderaudit5Form";
			}
			else{
				return "modules/stock/outOrderaudit6Form";
			}

		}
		return "modules/stock/outOrderForm";
	}

	@RequiresPermissions("stock:outOrder:edit")
	@RequestMapping(value = "save")
	public String save(OutOrder outOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, outOrder)){
			return form(outOrder, model);
		}
		outOrderService.save(outOrder);
		addMessage(redirectAttributes, "保存出库成功");
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	
	@RequiresPermissions("stock:outOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(OutOrder outOrder, RedirectAttributes redirectAttributes) {
		outOrderService.delete(outOrder);
		addMessage(redirectAttributes, "删除出库成功");
		return "redirect:"+Global.getAdminPath()+"/stock/outOrder/?repage";
	}

}